package org.convallaria.framework.swagger.util;

import io.swagger.v3.oas.models.Operation;
import io.swagger.v3.oas.models.media.Content;
import io.swagger.v3.oas.models.media.MediaType;
import io.swagger.v3.oas.models.media.Schema;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.responses.ApiResponses;

import java.util.Map;

/**
 * Swagger工具类
 * 
 * @author convallaria
 * @since 1.0.0
 */
public class SwaggerUtils {

    /**
     * 添加通用响应
     */
    public static void addCommonResponses(Operation operation) {
        ApiResponses responses = operation.getResponses();
        if (responses == null) {
            responses = new ApiResponses();
            operation.setResponses(responses);
        }

        // 成功响应
        responses.addApiResponse("200", new ApiResponse()
                .description("操作成功")
                .content(new Content()
                        .addMediaType("application/json", new MediaType()
                                .schema(new Schema<Object>()
                                        .$ref("#/components/schemas/ApiResult")))));

        // 业务异常响应
        responses.addApiResponse("400", new ApiResponse()
                .description("业务异常")
                .content(new Content()
                        .addMediaType("application/json", new MediaType()
                                .schema(new Schema<Object>()
                                        .$ref("#/components/schemas/ApiResult")))));

        // 认证失败响应
        responses.addApiResponse("401", new ApiResponse()
                .description("认证失败")
                .content(new Content()
                        .addMediaType("application/json", new MediaType()
                                .schema(new Schema<Object>()
                                        .$ref("#/components/schemas/ApiResult")))));

        // 权限不足响应
        responses.addApiResponse("403", new ApiResponse()
                .description("权限不足")
                .content(new Content()
                        .addMediaType("application/json", new MediaType()
                                .schema(new Schema<Object>()
                                        .$ref("#/components/schemas/ApiResult")))));

        // 系统异常响应
        responses.addApiResponse("500", new ApiResponse()
                .description("系统异常")
                .content(new Content()
                        .addMediaType("application/json", new MediaType()
                                .schema(new Schema<Object>()
                                        .$ref("#/components/schemas/ApiResult")))));
    }

    /**
     * 创建分页响应Schema
     */
    public static Schema<?> createPageResponseSchema(Schema<?> dataSchema) {
        Schema<Map<String, Object>> pageSchema = new Schema<>();
        pageSchema.setType("object");
        pageSchema.addProperty("code", new Schema<Object>().type("integer").example(200));
        pageSchema.addProperty("message", new Schema<Object>().type("string").example("操作成功"));
        pageSchema.addProperty("timestamp", new Schema<Object>().type("integer").format("int64"));
        
        Schema<Map<String, Object>> data = new Schema<>();
        data.setType("object");
        data.addProperty("records", new Schema<Object>().type("array").items(dataSchema));
        data.addProperty("total", new Schema<Object>().type("integer").format("int64"));
        data.addProperty("current", new Schema<Object>().type("integer"));
        data.addProperty("size", new Schema<Object>().type("integer"));
        data.addProperty("pages", new Schema<Object>().type("integer"));
        
        pageSchema.addProperty("data", data);
        return pageSchema;
    }

    /**
     * 创建列表响应Schema
     */
    public static Schema<?> createListResponseSchema(Schema<?> dataSchema) {
        Schema<Map<String, Object>> listSchema = new Schema<>();
        listSchema.setType("object");
        listSchema.addProperty("code", new Schema<Object>().type("integer").example(200));
        listSchema.addProperty("message", new Schema<Object>().type("string").example("操作成功"));
        listSchema.addProperty("timestamp", new Schema<Object>().type("integer").format("int64"));
        listSchema.addProperty("data", new Schema<Object>().type("array").items(dataSchema));
        return listSchema;
    }
}
